package com.qf.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Component
public class ApiCountInterceptor implements HandlerInterceptor {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        //1. 如何获取请求的 路径
        String requestURI = request.getRequestURI();

        //2. 在redis的hash数据结构中 ，对这个路径进行依次自增
        String key = "api:count";

        redisTemplate.opsForHash()
                .increment(key,requestURI,1);

        return true;

    }
}
